/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.chinalwb.are.provider;

import com.chinalwb.are.ResourceTable;
import com.chinalwb.are.models.ColorPickerModel;
import com.chinalwb.are.utils.FindUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;
import java.util.Optional;

/**
 * SimpleProvider
 *
 * @author Administrator
 * @since 2021/6/8
 */
public class ColorPickerProvider extends BaseItemProvider {
    private final List<ColorPickerModel> list;
    private final Context slice;

    public ColorPickerProvider(List<ColorPickerModel> list, Context slice) {
        this.list = list;
        this.slice = slice;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        if (list != null && position > 0 && position < list.size()) {
            return list.get(position);
        }
        return Optional.empty();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_color_item, null, false);
        } else {
            cpt = convertComponent;
        }
        Text name = FindUtils.findTextById(cpt, ResourceTable.Id_text);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbColor.fromArgbInt(Color.getIntColor(list.get(position).getColor())));
        name.setBackground(element);
        if (list.get(position).isSelect()) {
            name.setText("✓");
        } else {
            name.setText("");
        }
        return cpt;
    }

}
